<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
    <meta name="renderer" content="webkit" />

    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
    <meta name="format-detection" content="telephone=no" />
    <meta http-equiv="Expires" content="-1"/>

    <meta http-equiv="Cache-Control" content="no-cache"/>
    <meta http-equiv="Pragma" content="no-cache"/>
    <title>右键菜单</title>
    <link rel="shortcut icon" href="/favicon.ico"/>
</head>
<body>
    <style type="text/css">
      *{
        box-sizing: border-box;
        -moz-box-sizing:border-box;
        -webkit-box-sizing:border-box;
      }
      #container{
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        font-size: 14px;
        color: #999;
        padding: 4px;
        display: table-cell;
        text-align: center;
        vertical-align: middle;
      }
      p{
        vertical-align: middle;
      }
      #clientMenu{
        position: fixed;
        width: 80px;
        line-height: 20px;
        font-size: 12px;
        border: 1px solid #ccc;
        padding: 10px 4px;
        background-color: #fff;
        border-radius: 4px;
      }
      ul,li{
        list-style: none;
      }
      a{
        display: block;
        text-decoration: none;
        color: #666;
      }
      a:hover{
        background-color: #eee;
      }
    </style>
    <div id="test">
        <div id="container"><p>右键菜单区域</p></div>
        <ul id="clientMenu" style="visibility: hidden;">
          <li><a href="javascript:;">菜单 标题 1</a></li>
          <li><a href="javascript:;">菜单 标题 2</a></li>
          <li><a href="javascript:;">菜单 标题 2</a></li>
          <li><a href="javascript:;">菜单 标题 2</a></li>
          <li><a href="javascript:;">菜单 标题 2</a></li>
          <li><a href="javascript:;">菜单 标题 2</a></li>
          <li><a href="javascript:;">菜单 标题 2</a></li>
          <li><a href="javascript:;">菜单 标题 2</a></li>
          <li><a href="javascript:;">菜单 标题 2</a></li>
        </ul>
    </div>
    <script type="text/javascript">
      var container = document.getElementById('container');
      var clientMenu = document.getElementById('clientMenu');

      function menuEvent(event){
        var W = clientMenu.offsetWidth;
        var H = clientMenu.offsetHeight;
        if(event.clientX + W > screen.availWidth){
          clientMenu.style.left = event.clientX - W + 'px';
        }else{
          clientMenu.style.left = event.clientX + 'px';
        }
        if(event.clientY + H > screen.availHeight){
          clientMenu.style.top = event.clientY - H + 'px';
        }else{
          clientMenu.style.top = event.clientY + 'px';
        }
        clientMenu.style.visibility = 'visible';
        return false;
      }

      function hiddenMenu(){
        clientMenu.style.visibility = 'hidden';
      }

      function defaultMenu(){
        return false;
      }

      function init(){
        container.oncontextmenu = menuEvent;
        container.onclick = hiddenMenu;
        clientMenu.oncontextmenu = defaultMenu;
      }

      init();
    </script>
</body>
</html>
